var singUpController = angular.module('singUpController', [ 'ui.bootstrap' ])
		.controller('signUpHtmlController',
				[ '$scope', '$modal', '$location', signUpHtmlController ]);

function signUpHtmlController($scope, $modal) {
	$scope.Person = {
		name : '',
		surname : '',
		email : '',
		password : '',
		passwordConfirm : '',
		accept : ''
	};

	$scope.signUp = function() {

		if ($scope.Person.password == $scope.Person.passwordConfirm) {

			try {
				gapi.client.load('personendpoint', 'v1', null, 'https://robotapolar.appspot.com/_ah/api');
				gapi.client.personendpoint.insertPerson($scope.Person).execute(
						function(resp) {
							if (!resp.code) {
								$scope.Person.name='';
								$scope.Person.surname='';
								$scope.Person.email='';
								$scope.Person.password='';
								$scope.Person.passwordConfirm='';
								$scope.Person.accept='';
								$scope.singUpForm.$setPristine();
								window.location.href = "/secure/updatePersonalData.html";
							}
						});
			} catch (e) {
				var modalInstance = $modal.open({
					templateUrl : '../../secure/signupUnexpectedErrorDialog.html',
					controller : ModalInstanceCtrl
				});
			}

		} else {
			var modalInstance = $modal.open({
				templateUrl : '../../secure/signupPasswordErrorDialog.html',
				controller : ModalInstanceCtrl
			});
		}

	};
}

var ModalInstanceCtrl = function($scope, $modalInstance) {
	$scope.ok = function() {
		$modalInstance.close();
	};
}